package com.zhangye.test.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zhangye.test.model.Admin;
import com.zhangye.test.model.Proxy;
import com.zhangye.test.model.ProxyCompany;
import com.zhangye.test.util.HttpUtil;
import com.zhangye.test.util.PageInfo;
import com.zhangye.test.util.Result;

/**
 * 代理公司
 * 作者： 张叶
 * 创建时间：2018年9月20日  上午11:27:46
 */
@Controller
@RequestMapping("admin/proxyCompany")
public class ProxyCompanyController extends BaseController{

	
	/**
	 * 删除(删除代理公司，以及管理员)
	 * 作者： 张叶
	 * 创建时间：2018年5月16日  上午9:57:52
	 */
	@ResponseBody
	@RequestMapping(value = "/delProxyCompany/{id}", method = RequestMethod.POST)
	public Result delProxyCompany(@PathVariable Long id){
		Result result = new Result();
		if(id != null && !"".equals(id)){
			ProxyCompany pc = proxyCompanyService.selectById(id);
			if(pc != null){
				if(proxyCompanyService.deleted(pc) > 0){
					result.code = 0;
					result.message = "删除成功";
				}else{
					result.code = 1;
					result.message = "系统错误，删除失败";
				}
			}else{
				result.code = 4;
				result.message = "该记录或已被删除，请重新重试";
			}
		}else{
			result.code = 3;
			result.message = "参数不能为空";
		}
		return result;
	}
	
	
	/**
	 * 代表公司列表
	 * 作者： 张叶
	 * 创建时间：2018年9月20日  下午2:03:05
	 */
	@RequestMapping("proxyCompanyList")
	public String proxyList(Model model) {
		Admin admin = adminService.getCurrent();
		String where = "";
		if(admin.getProxyId() == null){
			List<Proxy> list = proxyService.selectWhere(where);
			model.addAttribute("list",list);
		}
		return "admin/proxyCompany/list";
	}
 
	
	/**
	 * 分页显示 
	 * 作者： 张叶
	 * 创建时间：2018年9月20日  下午2:03:23
	 */
	@ResponseBody
	@RequestMapping("proxyJson")
	public JSON proxyJson(PageInfo pageInfo, Integer rows, Integer page,
			String sidx, String sord,Long proxyId, Model model) {
		// 分页信息
		if (pageInfo == null) {
			pageInfo = new PageInfo();
		}
		if (pageInfo.getPage() < 2
				|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
			pageInfo.setCount(0);
		}
		if (rows != null) {// 每页显示条数
			pageInfo.setPageSize(rows);
		}
		if (page != null) {// 当前页
			pageInfo.setPage(page);
		}

		// 排序
		String order = " order by created_at desc";
		if (sidx != null && sidx != "") {// 排序字段
			order = " order by " + sidx + " ";
			if (sord != null && sord != "") {// 排序
				order += sord;
			}
		}

		// 搜索条件
		String where = "1 = 1";
		Admin admin = adminService.getCurrent();
		if(admin.getProxyId() != null && !"".equals(admin.getProxyId())){
			where += " and pc.proxy_id = " + admin.getProxyId();
		}
		if(proxyId != null && !"".equals(proxyId)){
			where += " and pc.proxy_id = " + proxyId;
		}
		where += " and status != 'deleted'";
		List<Map<String, Object>> list = proxyCompanyService.selectByPage(pageInfo,where, order);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (int i = 0; i < list.size(); i++) {
			Date createTime = (Date) list.get(i).get("created_at");
			list.get(i).put("created_at", sdf.format(createTime));

			Object shortUrl = list.get(i).get("short_link");
			if(shortUrl == null){
				Object urls = list.get(i).get("link");
				try{
					String url = HttpUtil.getShortUrl(urls.toString());
					Long id = (Long)list.get(i).get("id");
					ProxyCompany pc = proxyCompanyService.selectById(id);
					pc.setShortLink(url);
					proxyCompanyService.update(pc);
					list.get(i).put("short_link", url);
				}catch(Exception e){
					list.get(i).put("short_link", urls);
				}
			}

			Object shortUrlSpare = list.get(i).get("short_link_spare");
			if(shortUrlSpare == null){
				Object urls = list.get(i).get("link_spare");
				try{
					String url = HttpUtil.getShortUrl(urls.toString());
					Long id = (Long)list.get(i).get("id");
					ProxyCompany pc = proxyCompanyService.selectById(id);
					pc.setShortLinkSpare(url);
					proxyCompanyService.update(pc);
					list.get(i).put("short_link_spare", url);
				}catch(Exception e){
					list.get(i).put("short_link_spare", urls);
				}
			}
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("total", pageInfo.getTotalPage());
		data.put("list", list);
		return (JSON) JSON.toJSON(data);
	}

	
	/**
	 * 添加按钮，跳转到添加页面 
	 * 作者：张叶 
	 * 时间：2016年9月7日 上午11:59:36
	 */
	@RequestMapping("addProxyCompany")
	public String addProxy(Model model) {
		Admin admin = adminService.getCurrent();
		Long proxyId = admin.getProxyId();
		if(proxyId == null){
			List<Proxy> list = proxyService.selectWhere("");
			model.addAttribute("proxyList",list);
		}
		
		return "admin/proxyCompany/add";
	}

	
	/**
	 * 添加操作 
	 * 作者：张叶
	 * 时间：2016年9月7日 下午12:00:10
	 */
	@ResponseBody
	@RequestMapping("insertProxyCompany")
	public Result insertProxy(ProxyCompany pc) {
		Result result = new Result();
		if(pc.getProxyId() == null){
			Admin admin = adminService.getCurrent();
			pc.setProxyId(admin.getProxyId());
		}
		result = proxyCompanyService.insert(pc);
		return result;
	}
	
	
	/**
	 * 获取代理公司
	 * 作者： 张叶
	 * 创建时间：2018年10月10日  上午10:10:54
	 */
	@ResponseBody
	@RequestMapping("getProxyById/{id}")
	public List<ProxyCompany> getProxyById(@PathVariable Long id){
		String where = " and status != 'deleted' and proxy_id = " + id;
		List<ProxyCompany> list = proxyCompanyService.selectWhere(where);
		return list;
	}
	
}
